package com.study.date;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * simpledateFormat多线程问题
 * */
public class SimpleDataTest {


    @Test
    public void test1() throws Exception{
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Callable callable=()->format.parse("2022-04-23");
        test(callable);
    }

    /**
     * 使用threadLocal解决多线程问题
     * */
    @Test
    public void test2() throws Exception{
        Callable<Date> callable=()->ThreadLocatTest.getDate("2022-04-23");
        test(callable);
    }

    /**
     * 使用java8中线程安全类
     * */
    @Test
    public void test3() throws Exception{
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Callable<LocalDate> callable=()-> LocalDate.parse("2022-04-23",formatter);
        test(callable);
    }

    private void test(Callable callable) throws Exception{
        List<Future> list=new ArrayList<>();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for(int i=0;i<10;i++){
            list.add(executorService.submit(callable));
        }
        for (Future future : list) {
            System.out.println(future.get());
        }
    }

}
